package com.sinoglobal.ningxia.push_service;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import com.alibaba.fastjson.JSON;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.beans.MessageVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.thread.ItktOtherAsyncTask;
import com.sinoglobal.ningxia.utils.SharedPreferenceUtil;

/**
 * @author yrq
 * @createdate 2014-07-24 下午15:25:43
 * @Description: TODO 我的消息 推送消息
 */
public class PushService extends Service {
	private Timer mTimer;
	private TimerTask mTimerTask;
	private static long delay = 1000;
	private static long period = 1000 * 3600;
	private String imei;
	private String isPushMessageCheck = "1"; // 消息推送
	private String isActivityCheck = "1"; // 活动推送

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		StartTimer();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (mTimer != null) {
			mTimer.cancel();
		}

	}

	private void StartTimer() {
		if (mTimer == null) {
			mTimerTask = new TimerTask() {
				@Override
				public void run() {
					mHandler.sendEmptyMessage(1);
				}
			};
			mTimer = new Timer();
			mTimer.schedule(mTimerTask, delay, period);
		}
	}

	Handler mHandler = new Handler() {
		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			if (msg.what == 1) {
				getTuiSong();// TODO推送消息
			} else if (msg.what == 2) {
			}
		}
	};

	/**
	 * 推送消息
	 */
	public void getTuiSong() {
		imei = FlyApplication.getUniquely(getApplicationContext());
		if (SharedPreferenceUtil.getBoolean(FlyApplication.context,
				"isPushMessageCheck")) {
			isPushMessageCheck = "1";
		} else {
			isPushMessageCheck = "2";
		}
		if (SharedPreferenceUtil.getBoolean(FlyApplication.context,
				"isActivityCheck")) {
			isActivityCheck = "1";
		} else {
			isActivityCheck = "2";
		}
		/**
		 * 若 消息推送和 活动推送都不推送则不访问网络 否则会访问接口
		 */
		if (isPushMessageCheck.equals("2") && isActivityCheck.equals("2")) {

		} else {
			new ItktOtherAsyncTask<Void, Void, MessageVo>(false) {

				@Override
				public MessageVo before(Void... params) throws Exception {
					// 接口
					return RemoteImpl.getInstance().getMessageInfo(imei,
							isActivityCheck, isPushMessageCheck);
					// return JSON.parseObject(TestJson.getMessageVo(),
					// MessageVo.class);
				}

				@Override
				public void after(MessageVo result) {
					// ShowNotification(result);
					MessageVo oldRresultMessageVo = new MessageVo();
					/**
					 * 将消息保存到共享参数里面
					 * 
					 * 主注意共享参数里面的我的消息列表的内容的添加
					 */
					String messageString = SharedPreferenceUtil.getString(
							getApplicationContext(), "messageVo");
					if (messageString != null && messageString.length() > 0) {
						oldRresultMessageVo = JSON.parseObject(messageString,
								MessageVo.class);
					}
					if (result.getList() != null
							&& result.getList().size() != 0) {
						oldRresultMessageVo.getList().addAll(0,
								result.getList());
					}
					SharedPreferenceUtil
							.saveString(getApplicationContext(), "messageVo",
									JSON.toJSONString(oldRresultMessageVo));

				}

				// private void ShowNotification(MessageVo result) {
				// NotificationManager nfm = (NotificationManager)
				// getSystemService(NOTIFICATION_SERVICE);
				// Notification nfn = new Notification(R.drawable.ic_launcher,
				// result.getList().get(0).getTitle(),
				// System.currentTimeMillis() +
				// 20);
				// Intent intent = null;
				// intent = new Intent(getApplicationContext(),
				// MyMessageActivity.class);
				// // intent.putExtra("url", result.getUrl());
				// PendingIntent PendingIntentintent =
				// PendingIntent.getActivity(getApplicationContext(), 0, intent,
				// 0);
				// nfn.setLatestEventInfo(getApplicationContext(),
				// result.getList().get(0).getTitle(),
				// result.getList().get(0).getContent(), PendingIntentintent);
				// nfn.flags = Notification.FLAG_AUTO_CANCEL;
				// nfm.notify(id, nfn);
				// }

				@Override
				public void exception() {

				}
			}.execute();
		}
	}
}